package com.example.monitoringsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.monitoringsystem.bean.*;
import com.example.monitoringsystem.currentUser.StaticUser;
import com.example.monitoringsystem.mapper.DoctorMapper;
import com.example.monitoringsystem.mapper.HealthMapper;
import com.example.monitoringsystem.mapper.PatientMapper;
import com.example.monitoringsystem.mapper.UserMapper;
import com.example.monitoringsystem.service.FindPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class FindPatientServiceimpl implements FindPatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HealthMapper healthMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    //返回医生的所有患者表
    @Override
    public Object findPatientByDoctor(int doctorId) {
        List<Patient> patients=new ArrayList<> ();
        try {
            QueryWrapper<Patient> findPatient = new QueryWrapper<Patient> ();
            findPatient.eq ( "doctor", doctorId );
            patients= patientMapper.selectList ( findPatient );
        } catch (Exception e) {
        patients=null;
        }
        return  patients;
    }
    //返回医生的所有患者用户表
    @Override
    public Object findPatientUserByDoctor(int doctorId) {
        List<Patient> patients=new ArrayList<> ();
        List<User> users=new ArrayList<> ();
        try {
            QueryWrapper<Patient> findPatient = new QueryWrapper<Patient> ();
            findPatient.eq ( "doctor", doctorId );
            patients= patientMapper.selectList ( findPatient );
            for (int i = 0; i <patients.size (); i++) {
                QueryWrapper<User> findUser = new QueryWrapper<User> ();
                try {
                    findUser.eq ( "id",patients.get ( i ).getPatientId () );
                    users.add ( userMapper.selectOne ( findUser ) );
                } catch (Exception e) {
                    users.add ( null );
                }
            }
        } catch (Exception e) {
         patients=null;
        }
       return  users;
    }
    //返回管理员的所有患者用户表
@Override
public Object  findAllPatientUser() {
    List<Patient> patient;
    List<User> users=new ArrayList<> ();
    QueryWrapper<Patient> findPatient = new QueryWrapper<Patient> ();
    patient = patientMapper.selectList ( findPatient );
    if (patient==null)
    {
        users=null;
    }
    else
    {
        for (int i = 0; i < patient.size (); i++) {
            QueryWrapper<User> findUser = new QueryWrapper<User> ();
            try {
                findUser.eq ( "id", patient.get ( i ).getPatientId () );
                users.add ( userMapper.selectOne ( findUser ) );
            } catch (Exception e) {
                users.add ( null );
            }
        }
    }
    return  users;
}
    //返回管理员的所有用户表
    @Override
    public Object findAllPatient() {
        List<Patient> patient;
        QueryWrapper<Patient> findPatient = new QueryWrapper<Patient> ();
        patient = patientMapper.selectList ( findPatient );
        return  patient;
    }
}

